﻿Imports System.Data.OleDb


Public Class imageAddComments
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If System.Web.HttpContext.Current.User.Identity.IsAuthenticated Then


        Else
            Response.Redirect("imageComments.aspx")
        End If

        imgLike.Visible = False


        Dim sql As String = "SELECT * FROM Image_Likes WHERE UserID = @f1 AND ImageIDFK = @f2"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.Parameters.AddWithValue("@f2", Request.Params("id"))

        conn.Open()


        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        If dr.HasRows Then    'If there are 1 or more rows which are returned then hide the button and show the already liked image (thumbs up) otherwise hide the liked image and show the Like Button. 
            imgLike.Visible = True
            btnLike.Visible = False


        Else

            imgLike.Visible = False
            btnLike.Visible = True


        End If

        cmd.Dispose()
        conn.Close()
        conn.Dispose()

        Dim sql2 As String = "SELECT * FROM Image_Likes_Query WHERE ImageID=@f20"

        Dim conn2 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn2.Open()

        Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, conn2)

        cmd2.Parameters.AddWithValue("@f20", Request.Params("id"))

        Dim datareader = cmd2.ExecuteReader()


        datareader.Read()

        If Not IsDBNull(datareader("CountOfID")) Then lblTotalCounts.Text = datareader("CountOfID")





    End Sub



    Protected Sub btncomment_Click(sender As Object, e As EventArgs) Handles btncomment.Click

        If txtimagecomment.Text = "" Then
            MsgBox("You have not entered a comment, please check and try again.")

        Else
            Dim sql As String = "INSERT INTO Image_Comments (CommentBody,DateUploaded,TimeUploaded,UserID,ImageID) VALUES (@f1,@f2,@f3,@f4,@f5)"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@f1", txtimagecomment.Text)
            cmd.Parameters.AddWithValue("@f2", Now.Date.ToString("dd/MM/yyyy"))
            cmd.Parameters.AddWithValue("@f3", DateTime.Now.ToString("t"))
            cmd.Parameters.AddWithValue("@f4", Membership.GetUser(User.Identity.Name).ProviderUserKey)
            cmd.Parameters.AddWithValue("@f5", Request.QueryString("id"))

            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()

            Response.Redirect("imageAddComments.aspx?id=" & Request.QueryString("id"))

        End If




    End Sub


    Protected Sub btntagimage_Click(sender As Object, e As EventArgs) Handles btntagimage.Click
        Dim sql As String = "INSERT INTO Image_Tags (ImageIDFK,TagIDFK) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn.Open()
        For Each cb As ListItem In cbxtags.Items
            If cb.Selected = True Then
                'add this to the database
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("id"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()

            End If

        Next

        Response.Redirect("imageAddComments.aspx?id=" & Request.QueryString("id"))


    End Sub


    Protected Sub btnaddnewtag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnaddnewtag.Click
        Dim sql As String = "INSERT INTO Tags (TagName,UserIDFK) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", txtnewtag.Text)
        cmd.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.ExecuteNonQuery()

        Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT TOP 1 TagID FROM Tags ORDER BY TagID Desc", conn)
        Dim dr As OleDbDataReader = cmd2.ExecuteReader()
        dr.Read()
        Dim highestId = dr(0)

        Dim associate As String = "INSERT INTO Image_Tags (ImageIDFK,TagIDFK) VALUES (@f1,@f2)"
        Dim cmd3 As OleDbCommand = New OleDbCommand(associate, conn)
        cmd3.Parameters.AddWithValue("@f1", Request.QueryString("id"))
        cmd3.Parameters.AddWithValue("@f2", highestId)
        cmd3.ExecuteNonQuery()

        Response.Redirect("imageAddComments.aspx?id=" & Request.QueryString("id"))

    End Sub

    Protected Sub btnLike_Click(sender As Object, e As EventArgs) Handles btnLike.Click

        imgLike.Visible = True
        btnLike.Visible = False


        Dim sql As String = "INSERT INTO Image_Likes (ImageIDFK,UserID) VALUES (@f1,@f2)"

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)


        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", Request.Params("id"))
        cmd.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)

        conn.Open()

        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        Response.Redirect("imageAddComments.aspx?id=" & Request.QueryString("id"))

    End Sub

    Protected Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged

    End Sub
End Class
